layui.use(['layer', 'form', 'table'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var form = layui.form;
    var table = layui.table;

    /**
     * 集团信息--集团智能网语音信息管理
     */
    var SmartVoice = {
        tableId: "smartVoiceTable",    //表格id
        condition: {
            accountId: ""
        }
    };

    /**
     * 初始化表格的列
     */
    SmartVoice.initColumn = function () {
        return [[
            {field: 'groupId', align: "center", width:190, title: '群组ID'},
            {field: 'groupName', align: "center", title: '群组名称'},
            {field: 'groupTypeName', align: "center", width:120, title: '群组类型'},
            {field: 'thresRangeName', align: "center", title: '企业通话阈值的计费范围'},
            {field: 'forwardNumber', align: "center", title: '自动前转特服号码（呼叫转移）'},
            {field: 'calledLockName', align: "center", width:150, title: '限制闭锁'},
            {align: 'center', toolbar: '#callParamListBar', width:180, title: '企业客户成员通话参数'},
            {field: 'callParamList', hide: true, title: '企业客户成员通话参数列表'}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SmartVoice.tableId,
        defaultToolbar: [],
        url: Feng.ctxPath + '/smartVoice/list',
        page: true,
        limits: [10,20,30,40,50],
        height: "full-98",
        cellMinWidth: 100,
        cols: SmartVoice.initColumn(),
        done:function(res, curr, count){
            // 鼠标显示过长的表头
            tdTitle();
        }
    });

    function tdTitle() {
        $('th').each(function(index,element){
            $(element).attr('title',$(element).text());
        });
        $('td').each(function(index,element){
            $(element).attr('title',$(element).text());
        });
    }

    /**
     * 点击查询按钮
     */
    $("#btnSearch").click(function () {
        SmartVoice.search();
    });
    SmartVoice.search = function () {
        let queryData = {};
        let accountId = $("#accountName").val();
        queryData['accountId'] = accountId;
        table.reload(SmartVoice.tableId, {
            where: queryData, page: {curr: 1}
        });
        initAccountSelect(accountId);
    };

    // 工具条点击事件
    table.on('tool(' + SmartVoice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'callParamList') {
            SmartVoice.callParamListBar(encodeURIComponent(JSON.stringify(data.callParamList)));
        }
    });

    /**
     * 查看 企业客户成员通话参数
     *
     * @param data 点击按钮时候的行数据
     */
    SmartVoice.callParamListBar = function (data) {
        layer.open({
            type: 2,
            title: '企业客户成员通话参数',
            area: ['700px', '550px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/smartVoice/callParam?callParam=' + data,
            btn: ['关闭']
        });
    };

    initAccountSelect();

    /**
     * 初始化集团下拉列表框
     * @type {layui.ax}
     */
    function initAccountSelect(accountId) {
        $('#accountName').html('');
        $("#accountName").append('<option value="" >请选择</option>');
        $.ajax({
            dataType: 'json',
            type: 'post',
            url: Feng.ctxPath + "/account/allSelectAccounts",
            success: function (data) {
                for (let i = 0; i < data.data.length; i++) {
                    let name = data.data[i].accountName;
                    let code = data.data[i].accountId;
                    if (accountId == code) {
                        $("#accountName").append('<option value="' + code + '" selected>' + name + '</option>');
                    } else {
                        $("#accountName").append('<option value="' + code + '">' + name + '</option>');
                    }
                }
                form.render('select');
            }
        });
    }

});